unit UCriptografia;

interface

uses Dialogs;

type
  TCriptografia = class

    private
      FCriptografia: String;
      FDecriptografia: String;
      FOperation: Boolean;
      FConstCripto: String; 
      FConstDeCrip: String;

      function Encripta(sTexto: string): String;
      function Decripta(sTexto: string): String;

      function GetCritografia: String;
      function GetDecritografia: String;

      procedure SetCriptografia(const Value: String);
      procedure SetDecriptografia(const Value: String);

    protected
      procedure SetValidOperation(const Value: Boolean);

    public
      constructor Create;
      function GetValidOperation: Boolean;
      property Criptografia: String read GetCritografia write SetCriptografia;
      property Decriptografia: String read GetDecritografia write SetDecriptografia;
  end;

implementation

uses SysUtils;

{ TCriptografia }

constructor TCriptografia.Create;
begin
  FConstCripto := 'IA1BwCFG2JKEM-3OPQRHSTU6VyWDYZ8ablcd4efXghrij5Lkno7+qst9uvxmz0p/N ';
  FConstDeCrip := '+S-1VA9B2DE3FeGH4IJaK5LM/N6OPpQ7RTs8CUWXYZbcfxghi0jklmdoqrtuvwynz ';
  FOperation := False;
end;

function TCriptografia.Decripta(sTexto: string): String;
  var nPos,k: integer; sStr, sDecrip: String;
begin
  sDecrip:='';
  for k:= 1 to Length(sTexto) do
  begin
    nPos    := POS(COPY(sTexto,k,1), FConstDeCrip);
    sStr    := COPY(FConstCripto,nPos,1);
    sDecrip := sDecrip + sStr;
  end;
  Result := sDecrip;

end;

function TCriptografia.Encripta(sTexto: string): String;
  var nPos,k: integer; sStr, sCripto: String;
begin
  sCripto := '';
  for k:= 1 to Length(sTexto) do
  begin
    nPos    := POS(COPY(sTexto,k,1), FConstCripto);
    sStr    := COPY(FConstDeCrip,nPos,1);
    sCripto := sCripto + sStr;
  end;
  Result := sCripto;

end;

function TCriptografia.GetCritografia: String;
begin
  Result := FCriptografia;
end;

function TCriptografia.GetDecritografia: String;
begin
  Result := FDecriptografia;
end;

procedure TCriptografia.SetCriptografia(const Value: String);
begin
  if(Value = '')then
  begin
    Self.SetValidOperation(False);
  end
  else
  begin
    FCriptografia := Self.Encripta(Value);
    Self.SetValidOperation(True);
  end;
end;

procedure TCriptografia.SetDecriptografia(const Value: String);
begin
  if(Value = '')then
  begin
    Self.SetValidOperation(False);
  end
  else
  begin
    FDecriptografia := Self.Decripta(Value);
    Self.SetValidOperation(True);
  end;
end;

procedure TCriptografia.SetValidOperation(const Value: Boolean);
begin
  FOperation := Value;
end;

function TCriptografia.GetValidOperation: Boolean;
begin
  Result := FOperation;
end;

end.
